package com.bdqn.controller;

import com.bdqn.Service.Emplyee.EmployeeService;
import com.bdqn.Service.SysMenus.SysMenusService;
import com.bdqn.Service.SysUsers.SysUsersService;
import com.bdqn.entity.Employee;
import com.bdqn.entity.SysMenus;
import com.bdqn.entity.SysUsers;
import com.bdqn.mapper.SysMenus.SysMenusMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/user")
public class SysUsersController {
    @Autowired
    SysUsersService usersService;
    @Autowired
    SysMenusService menusService;
    @Autowired
    EmployeeService employeeService;
    @PostMapping("/login")
    @ResponseBody
    public Result login(SysUsers sysUsers, Model model){
//        sysUsers.setLoginId("admin");//
//        sysUsers.setPassword("123456");//开发时使用 上线时删除
        Subject subject= SecurityUtils.getSubject();
        UsernamePasswordToken token=new UsernamePasswordToken(sysUsers.getLoginId(),sysUsers.getPassword());
        subject.login(token);
        SysUsers byName = usersService.findByName(sysUsers.getLoginId());
        subject.getSession().setAttribute("user",byName);
        List<SysMenus> menus = menusService.findMenus();
        subject.getSession().setAttribute("Menus",menus);//用户
        Employee employee = employeeService.fingByid(byName.getId());
        subject.getSession().setAttribute("employee",employee);//员工

        return new Result(Code.GET_OK,sysUsers,"登录成功!");
    }
    //注销用户
    @GetMapping("/logout")
    public String logout(){
        //使用Shiro注销用户
        SecurityUtils.getSubject().logout();
        return "redirect:/";
    }
}
